// 创建角色相关的小仓库
import { defineStore } from "pinia";
import type { RoleState } from "./types/roleType";
import { reqAddRole, reqRoleList, reqUpdateRole} from "@/api/role"
import type { addRoleResponseData, getRoleListResponseData,roleInfoData } from "@/api/role/type";
// import { reqUpdatePwd } from "@/api/user";
// 创建角色小仓库
let useRoleStore = defineStore("Role",{
    state:():RoleState=>{
        return{
            roles:[]
        }
    },
    actions:{
        async getRoleList(){
            let result:getRoleListResponseData = await reqRoleList();
            if(result.status == 0){
                // @ts-ignore
                this.roles = result.data as roleInfoData[]
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 添加角色
        async addRole(data:roleInfoData){
            const result:addRoleResponseData = await reqAddRole(data);
            if(result.status == 0){
                this.getRoleList()
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 设置角色权限
        async updateRole(data:roleInfoData){
            const result:addRoleResponseData = await reqUpdateRole(data);
            if(result.status == 0){
                this.getRoleList()
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
    },
    getters:{

    }
})
// 对外暴露角色小仓库方法
export default useRoleStore